package hn.sigit.logic.ladm;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import hn.sigit.util.PropertyRightsComparable;
import hn.sigit.util.RightsComparable;

public class RightsUtil {
	public static boolean propertyRightsEqual(PropertyRightsComparable prc1, PropertyRightsComparable prc2) {
		if (prc1.propertyRightsHash() == prc2.propertyRightsHash()) {
			Map<Integer, RightsComparable> rightsMap = new HashMap<Integer, RightsComparable>();
			for (RightsComparable rc : prc1.getRightsComparableList())
				rightsMap.put(rc.rightsHash(), rc);
			
			Map<Integer, RightsComparable> otherRightsMap = new HashMap<Integer, RightsComparable>();
			for (RightsComparable rc : prc2.getRightsComparableList())
				otherRightsMap.put(rc.rightsHash(), rc);
			
			if ( rightsMap.keySet().equals( otherRightsMap.keySet() ) )
				for (RightsComparable rc : rightsMap.values())
					if (!rc.rightsEquals(otherRightsMap.get(rc.rightsHash())))
						return false;
			
			return true;
		}
		return false;
	}
	
	public static boolean rightsEqual(RightsComparable rc1, RightsComparable rc2) {
		if (rc1.rightsHash() == rc2.rightsHash()) {
			return rc1.getShare().hashCode() == rc2.getShare().hashCode()
					&& rc1.getExtPID() == rc2.getExtPID()
					&& rc1.getType().equals(rc2.getType());
		}
		return false;
	}
	
	public static boolean propertyHasRight(PropertyRightsComparable theProperty, RightsComparable right) {
		List<RightsComparable> rightList = theProperty.getRightsComparableList();
		for (RightsComparable rc : rightList) {
			if (rightsEqual(rc, right))
				return true;
		}
		
		return false;
	}
}
